from PySide2.QtWidgets import QApplication, QMessageBox,QLabel
from PySide2.QtUiTools import QUiLoader
from PySide2.QtGui import QPixmap
import matplotlib.pyplot as plt
import numpy as np


class Stats:

    def __init__(self):
        # 从文件中加载UI定义
        # 从 UI 定义中动态 创建一个相应的窗口对象
        # 注意：里面的控件对象也成为窗口对象的属性了
        # 比如 self.ui.button , self.ui.textEdit
        self.ui = QUiLoader().load('ui/window.ui')
        self.ui.pushButton.clicked.connect(self.plotdraw)

    def plotdraw(self):
        # 从文本框获取，而不作为函数输入
        # 绘制一条线
        xpoint = self.ui.plainTextEdit.toPlainText()
        ypoint = self.ui.plainTextEdit_2.toPlainText()
        xpoints = xpoint.split()
        ypoints = ypoint.split()

        if xpoint or ypoint == ['']:
            if xpoint==['']:
                PixMap = QPixmap(plt.plot(ypoints))
                label_7.setPixmap(PixMap)
                #plt.plot(ypoints)
            else :
                PixMap = QPixmap(str(plt.plot(xpoints)))
                label_7.setPixmap(PixMap)
                #plt.plot(xpoints)
        elif xpoint and ypoint !=['']:
            #[1, 8], [3, 10]  # (1,3)-(8,10)
            PixMap = QPixmap(plt.plot(xpoints, ypoints))
            label_7.setPixmap(PixMap)
            #plt.plot(xpoints, ypoints)
        else:  # xy点均孔
            print('请至少输入一组坐标')
            QMessageBox.warning(
                self.ui,
                '坐标错误',
                '请至少输入一个x或者y坐标')
        plt.show()


if __name__ == "__main__":
    app = QApplication([])
    label_7=QLabel()
    #PixMap=QPixmap()

    # 实例化窗口
    label_7.show()
    mainwindow = Stats()
    mainwindow.ui.show()
    app.exec_()
